<?php

require_once dirname(__FILE__) . '/config.php';
require_once 'functions/sql.php';
$sql = new sql();
$sql->connect();
// Set up the IPP instance
$IPP = new QuickBooks_IPP($dsn);
// Get our OAuth credentials from the database
$creds = $IntuitAnywhere->load($the_username, $the_tenant);

// Tell the framework to load some data from the OAuth store
$IPP->authMode(
        QuickBooks_IPP::AUTHMODE_OAUTH, $the_username, $creds);

// This is our current realm
$realm = $creds['qb_realm'];

// Load the OAuth information from the database
if ($Context = $IPP->context()) {
    // Set the IPP version to v3 
    $IPP->version(QuickBooks_IPP_IDS::VERSION_3);
    $ItemService = new QuickBooks_IPP_Service_Item();
    $count = count($_SESSION['shoping_cart']);
    $product_coupon_name = '';
	$product_coupon_price = '';
	if(isset($_SESSION['coupon_detail'])){
		//Add coupon as a product in QB
		$product_coupon_name = "Discount Code - ".$_SESSION['coupon_detail']['coupon_code'];
		$product_coupon_price = "-".$_SESSION['coupon_detail']['coupon_price'];
    }
	
	$Item = new QuickBooks_IPP_Object_Item();
    $Item->setName($product_coupon_name);
    $Item->setIncomeAccountRef('49');
    $Item->setUnitPrice($product_coupon_price);
    $items = $ItemService->query($Context, $realm, "SELECT * FROM Item WHERE Name = '" . $product_coupon_name . "' ");
    $quick_book_id = '';
    if (count($items) > 0) {
        foreach ($items as $Item) {
            $quick_book_id = $Item->getId();
            $Item->setUnitPrice($product_coupon_price);
            $updateresp = $ItemService->update($Context, $realm, $Item->getId(), $Item);
        }
    }
    if ($quick_book_id != '') {
        $resp = $quick_book_id;
    }else {
        $resp = $ItemService->add($Context, $realm, $Item);
    }
        
        
        
        
        
    
    
    foreach ($_SESSION['shoping_cart'] as $key => $value) {
        $Item = new QuickBooks_IPP_Object_Item();
        $product_name = $value['prd_name'] . "[" . $value['prd_code'] . "]";
        $Item->setName($product_name);
        $Item->setDescription($value['prd_name']);
        $Item->setIncomeAccountRef('49');
        $Item->setUnitPrice($value['prd_price']);
        $items = $ItemService->query($Context, $realm, "SELECT * FROM Item WHERE Name = '" . $product_name . "' ");
        $quick_book_id = '';
        if (count($items) > 0) {
            foreach ($items as $Item) {
                $quick_book_id = $Item->getId();
                $Item->setDescription($value['prd_name']);
                $Item->setUnitPrice($value['prd_price']);
                $updateresp = $ItemService->update($Context, $realm, $Item->getId(), $Item);
            }
        }
        if ($quick_book_id != '') {
            $resp = $quick_book_id;
        } else {
            $resp = $ItemService->add($Context, $realm, $Item);
        }
        //echo $resp;exit;
        if ($resp) {
            $product_id = $resp;
            /*$updatedata = array(
                "quick_book_id" => $product_id,
            );*/
            //$update = $sql->update('tbl_product', $updatedata, array("prd_id" => $value['prd_id']));

         } else {
            print($ItemService->lastError($Context));
        }
    }
    
} else {
    die('Unable to load a context...?');
}
?>
